using Comteck.Winforms.Controls.Loading;
using Comteck.Winforms.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace Comteck.Winforms.Controls {

  /// <summary>
  /// 用户控件 - 基类
  /// </summary>
  public partial class UcBase : XtraUserControl {

    /// <summary>
    /// 
    /// </summary>
    /// <param name="text">提示信息</param>
    /// <param name="addToLog">是否添加到日志中</param>
    /// <param name="exception">异常信息</param>
    /// <returns></returns>
    protected DialogResult ShowMsg(string text, params object[] args) {
      return DevHelper.ShowMsg(text, args);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="text"></param>
    /// <param name="caption"></param>
    /// <param name="buttons"></param>
    /// <param name="icon"></param>
    /// <returns></returns>
    protected DialogResult ShowMsg(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon) {
      return DevHelper.ShowMsg(text, caption, buttons, icon);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="text"></param>
    /// <param name="caption"></param>
    /// <param name="buttons"></param>
    /// <param name="icon"></param>
    /// <returns></returns>
    protected DialogResult ShowMsg(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton) {
      return DevHelper.ShowMsg(text, caption, buttons, icon, defaultButton);
    }

    /// <summary>
    /// 标题：获取一个值，用以指示 System.ComponentModel.Component 当前是否处于设计模式。
    /// 描述：DesignMode 在 Visual Studio 产品中存在 Bug ，使用下面的方式可以解决这个问题。
    /// </summary>
    protected internal new bool DesignMode {
      get {
        bool returnFlag = false;
#if DEBUG
        if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime) {
          returnFlag = true;
        } else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToUpper().Equals("DEVENV")) {
          returnFlag = true;
        }
#endif
        return returnFlag;
      }
    }

    #region properties

    /// <summary>
    /// 每行是否显示一个复选框
    /// </summary>
    [DefaultValue(false), Category("功能扩展"), Description("每行是否显示一个复选框"), ReadOnlyAttribute(false)]
    public virtual bool IsShowCheckBox { get; set; }

    /// <summary>
    /// 主键(主要在编辑用) add by weijie.sun 2013/10/12
    /// </summary>
    protected virtual object EntityKey { get; set; }

    /// <summary>
    /// 
    /// </summary>
    private WinSelectType _SelectType = WinSelectType.Multi;

    /// <summary>
    /// 选择记录的类型 add by weijie.sun 2013/10/26
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Never)]
    [Browsable(false)]
    public virtual WinSelectType SelectType {
      get { return this._SelectType; }
      set { this._SelectType = value; }
    }

    /// <summary>
    /// 删除的记录行
    /// </summary>
    public List<long> DeletedRows = new List<long>();

    /// <summary>
    /// 是否自动绑定数据源
    /// </summary>
    [DefaultValue(true), Category("功能扩展"), Description("是否自动绑定数据源"), ReadOnly(false)]
    public virtual bool AutoBindData { get; set; } = true;

    /// <summary>
    /// 是否受用户权限控制
    /// </summary>
    [DefaultValue(true), Category("功能扩展"), Description("是否受用户权限控制"), ReadOnly(false)]
    public virtual bool IsControlledByAuth { get; set; } = true;

    /// <summary>
    /// 当前模式，是否是编辑还是纯显示
    /// </summary>
    [Browsable(false)]
    protected virtual bool IsEditMode { get; set; } = false;

    /// <summary>
    /// 
    /// </summary>
    protected override CreateParams CreateParams {
      get {
        var parms = base.CreateParams;
        //parms.ExStyle |= 0x02000000;
        parms.Style &= ~0x02000000; // Turn off WS_CLIPCHILDREN 
        return parms;
      }
    }

    /// <summary>
    /// ajax加载框
    /// </summary>
    protected AjaxLoadHelper ajaxLoad = null;

    /// <summary>
    /// ajax加载框
    /// </summary>
    /// <param name="ajaxLoad"></param>
    public void SetAjaxLoadHelper(AjaxLoadHelper ajaxLoad) {
      this.ajaxLoad = ajaxLoad;
    }

    #endregion

    #region constructor

    /// <summary>
    /// Create a new InitializeComponent object
    /// </summary>
    public UcBase() {
      InitializeComponent();
    }

    #endregion

    #region methods

    /// <summary>
    /// 初始化列表选择框
    /// </summary>
    /// <param name="gridview">GridView</param>
    /// <param name="column">GridColumn</param>
    public void InititalUC(GridView gridview, GridColumn column) {
      this.InititalUC(gridview, column, this.SelectType);
    }

    /// <summary>
    /// 初始化列表选择框
    /// </summary>
    /// <param name="gridview">GridView</param>
    /// <param name="column">GridColumn</param>
    public void InititalUC(GridView gridview, GridColumn column, WinSelectType type) {
      // 是否显示
      column.Visible = this.IsShowCheckBox;

      // 复选框
      switch (type) {
        case WinSelectType.Single:
          // 单选
          gridview.AddCheckBoxSingle(column);
          break;
        default:
          // 复选框
          gridview.AddCheckBoxHeadCell(column);
          break;
      }

      // 复制单元格
      gridview.EnableCopyCell();
    }

    #endregion

    #region events

    /// <summary>
    /// 窗口加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void UcBase_Load(object sender, EventArgs e) {
      if (this.DesignMode) {
        return;
      }
    }

    #endregion
  }
}
